#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->treeWidget->setHeaderLabel("高考");

    QTreeWidgetItem* it1 = new QTreeWidgetItem();
    it1->setText(0,"语文");
    ui->treeWidget->addTopLevelItem(it1);

    QTreeWidgetItem* it2 = new QTreeWidgetItem();
    it2->setText(0,"数学");
    ui->treeWidget->addTopLevelItem(it2);

    QTreeWidgetItem* it3 = new QTreeWidgetItem();
    it3->setText(0,"外语");
    ui->treeWidget->addTopLevelItem(it3);

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_addtop_clicked()
{
    const QString& text = ui->lineEdit->text();

    if(text.isEmpty())
    {
        return;
    }

    QTreeWidgetItem* it = new QTreeWidgetItem();
    it->setText(0,text);

    ui->treeWidget->addTopLevelItem(it);
}

void Widget::on_pushButton_add_clicked()
{
    const QString& text = ui->lineEdit->text();
    if(text.isEmpty())
    {
        return;
    }

    QTreeWidgetItem* cur = ui->treeWidget->currentItem();
    if(cur == nullptr)
    {
        return;
    }

    QTreeWidgetItem* it = new QTreeWidgetItem();
    it->setText(0,text);

    cur->addChild(it);
    cur->setExpanded(true);
}

void Widget::on_pushButton_del_clicked()
{
    QTreeWidgetItem* del = ui->treeWidget->currentItem();
    if(del == nullptr)
    {
        return;
    }
    QTreeWidgetItem* parent = del->parent();
    if(parent == nullptr)
    {
        int i = ui->treeWidget->indexOfTopLevelItem(del);
        ui->treeWidget->takeTopLevelItem(i);
    }
    else
    {
        parent->removeChild(del);
    }

}
